package common.utils.net;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import common.utils.properties.PropertiesUtil;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;

public class DownloadHandler implements Runnable{
	
	private String httpPath;
	private Handler progressHandler;
	private String fileName;
	public DownloadHandler(String httpPath,Handler progressHandler,String fileName){
		this.httpPath = httpPath;
		this.progressHandler = progressHandler;
		this.fileName = fileName;
	}
	public void run() {
		downLoadServiceXML(httpPath);
	}
	
	
	private void downLoadServiceXML(String httpPath) {
		FileOutputStream fos = null;
		InputStream fis = null;
		HttpURLConnection httpUrlConnection = null;
		try {
			URL httpUrl = new URL(httpPath);
			File fileDirectory = getImageCacheDirectory();
			if(!fileDirectory.exists()){
				fileDirectory.mkdirs();
			}
			File xmlFile = new File(fileDirectory,fileName);
			httpUrlConnection = (HttpURLConnection) httpUrl.openConnection();
			
			Float bytesum = 0f;
			int httpState = 404;
			
			try {
				httpState=httpUrlConnection.getResponseCode();
			} catch (ConnectException e) {
				e.printStackTrace();
			}
			
			if(httpState<400){
				
				if(xmlFile.exists()){
					xmlFile.delete();
				}
				
				fos = new FileOutputStream(xmlFile);
				httpUrlConnection.connect();
				
				Float wholeContent = new Float(httpUrlConnection.getContentLength());
				
				fis = httpUrlConnection.getInputStream();
				int i = 0;
				byte[] b = new byte[64];
				
				while((i=fis.read(b)) != -1){
					fos.write(b, 0, i);
					bytesum += i;
					sendMsg(Math.round(bytesum*100/wholeContent));
				}
				sendMsg(-1);
			}else{
				sendMsg(-2);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(fis != null){
					fis.close();
				}
				if(fos != null){
					fos.flush();
					fos.close();
				}
				httpUrlConnection.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void sendMsg(int flag) {
		Message msg = new Message();
		msg.obj = flag;
		progressHandler.sendMessage(msg);
	}
	
	private File getImageCacheDirectory(){
//		if(!isSdcardPrepared()){
			return new File(Environment.getDataDirectory(), PropertiesUtil.getInstance().getValue("qr_xml_file_cache"));
//		}else{
//			return new File(Environment.getExternalStorageDirectory(), "/service/com.techmore.pls/xmlCache");
//		}
	}
	
//	private boolean isSdcardPrepared(){
//		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
//	}

}
